﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDT.DataService.Contexts;
using SDT.DataService.Repositories;
using SDT.DataService.Services;
using SDT.CrawlSystem.Servers;
using SDT.Libraries.Utilities;

namespace SDT.DataService.Services
{
    public class PageService : IService<Spider_Page>
    {
        #region Field      
        private IRepository<Spider_Page> repository;
        #endregion

        #region Constructor
        public PageService()
            : this(new Repository<Spider_Page>())
        {
        }

        public PageService(IRepository<Spider_Page> repository)
        {
            this.repository = repository;           
        }
        #endregion

        #region Method
       
        /// <summary>
        /// Database : NewsSpider
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="cateID"></param>
        /// <param name="url"></param>
        /// <param name="imgDes"></param>
        /// <param name="name"></param>
        /// <param name="wID"></param>
        /// <returns></returns>
        public int InsertPage(int cid, int cateID, string url, string imgDes, string name, string wID)
      {
            PageContext contextObj = new PageContext();
            try 
	
            {
                return contextObj.InsertPage(cid, cateID, url,imgDes, name, wID);
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertPage", ex.Message));
                throw;
            }
            finally {
                contextObj.Dispose();
                
            }
            

        }
                
        public int UpdatePage(int pageID, string name)
        {
            PageContext contextObj = new PageContext();
            try
            {
                return contextObj.UpdatePage(pageID, name);
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "PageService.UpdatePage", ex.Message));
                throw;
            }
            finally { contextObj.Dispose(); }
            

        }

        public int Pages_Count_List(int cateid, string status, byte nRetryFrom, byte nRetryTo)
        {
            PageContext contextObj = new PageContext();
            try
            { 
              return contextObj.sp_WC_Pages_Count_List(cateid,status,nRetryFrom,nRetryTo);

            }
            catch (Exception ex)
            {
               new LogException().Write(string.Format("  Module {0} --Msg {1} ", "Pages_Count_List", ex.Message));

                throw;        
            }
            finally { contextObj.Dispose(); }

        
        }

        public bool ChangeStatus(int pageid, byte status)
        {
            PageContext contextObj = new PageContext();
            try
            {
                return contextObj.sp_WC_PageChangeStatus(pageid, status) > 1;
            }
            catch (Exception ex)
            {
             new LogException().Write(string.Format("  Module {0} --Msg {1} ","ChangeStatus",ex.Message));
             throw;    
            }
            finally { contextObj.Dispose(); }
            
        
        }

        public  int CountList(UrlLoader uFilter)
        {
            PageContext contextObj = new PageContext();
            try
            {
                return contextObj.sp_WC_Pages_Count_List(uFilter.PID, uFilter.StrStatus, (byte)uFilter.RetryFrom, (byte)uFilter.RetryTo);
            }
            catch (Exception ex) 
            { 
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "CountList", ex.Message));
                throw;
                
            }
            finally { contextObj.Dispose(); }
            
          
        }

        private IList<sp_WC_Pages_SelectListResult> SelectList(UrlLoader uFilter)
        {
            PageContext contextObj = new PageContext();
            try
            {
                var rows = contextObj.sp_WC_Pages_SelectList(
                   (int)uFilter.PID,
                    uFilter.StrStatus,
                    (byte)uFilter.RetryFrom,
                    (byte)uFilter.RetryTo,
                    (short)uFilter.PageSize,
                    (int)uFilter.CurrentPage).ToList();

                return rows;               
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "PageService.GetSelectList", ex.Message));

            }
            finally
            {
                contextObj.Dispose();
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uFilter"></param>
        /// <returns></returns>
        public IList<WebWorkUnit> GetSelectList(UrlLoader uFilter)
        {  
            try
            {
                var rows = SelectList(uFilter);

                if (rows != null && rows.Count > 0)
                {
                    var l = new List<WebWorkUnit>(rows.Count);
                    foreach (var item in rows)
                    {
                        var u = LoadById(item.PageId, uFilter);
                        if (u != null)
                            l.Add(u);
                    }
                    return l;
                }

                return new List<WebWorkUnit>();
            }
            catch (Exception ex) 
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "PageService.GetSelectList", ex.Message));                
               
            }           
            return null;
             
        }

        public WebWorkUnit LoadById(int uid, UrlLoader uFilter)
        {
            PageContext contextObj = new PageContext();
            try
            {
                var u = contextObj.sp_WC_Pages_SelectByID((int)uid).First();
                if (u != null)
                    return new WebWorkUnit(u.PageID, u.SiteID, uFilter, u.Url);
                return null;
            }
            catch (Exception ex) 
            { new LogException().Write(string.Format("  Module {0} --Msg {1} ", "PageService.GetSelectList", ex.Message));
            throw;
            }
            finally { contextObj.Dispose(); }
        }


        #endregion


        #region IService<Spider_Page> Members

        public IQueryable<Spider_Page> GetAll()
        {
            throw new NotImplementedException();   
        }

        public bool Update(Spider_Page entity)
        {
            throw new NotImplementedException();
        }

        public bool Create(Spider_Page entity)
        {
            throw new NotImplementedException();
        }

        public bool Delete(Spider_Page entity)
        {
            throw new NotImplementedException();
        }

        #endregion

     
    }
}
